package com.yhx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yhx.iservice.IOrderSettingService;
import com.yhx.mapper.OrdersettingMapper;
import com.yhx.pojo.CalenderVo;
import com.yhx.pojo.Ordersetting;
import com.yhx.utils.MessageConstant;
import com.yhx.utils.Result;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Service
public class IOrderSettingServiceImpl implements IOrderSettingService {
    @Resource
    private OrdersettingMapper ordersettingMapper;
    @Override
    public Result save(List<Ordersetting> list) {
        if(list==null){
            return new Result(false, "检查表中是否有数据");
        }
        //得到前端传递过来的所有datelist
        ArrayList<Date> datalist = new ArrayList<>();
        for (Ordersetting ordersetting : list) {
            datalist.add(ordersetting.getOrderdate());
        }
        QueryWrapper<Ordersetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("orderDate", datalist);
        //数据库中找出来的重复的数据
        List<Ordersetting> dblist = ordersettingMapper.selectList(queryWrapper);
        //存在重复数据进行修改
       if(dblist!=null ||dblist.size()!=0){
           for (Ordersetting ordersetting : dblist) {
               //已经预约的人数
               Integer reservations = ordersetting.getReservations();
               //拿到前端传递过来的Number
               Integer number = getNumber(list, ordersetting.getOrderdate());
               //修改人数大于已经预约的人数
               if (number >= reservations) {
                   ordersetting.setNumber(number);
               }
               ordersettingMapper.updateById(ordersetting);
           }
           //求到的时数据库不存在的数据
           list.removeAll(dblist);
       }
        //数据库中不存在的数据进行添加
        if(list.size()>0){
            for (Ordersetting ordersetting : list) {
                ordersettingMapper.insert(ordersetting);
            }
        }
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }


    public Integer getNumber(List<Ordersetting> list,Date date){
        for (Ordersetting ordersetting : list) {
            if(ordersetting.getOrderdate().equals(date)){
                return ordersetting.getNumber();
            }
        }
        return 0;
    }

    @Override
    public Result list(String time) {
        String startTime=time+"-01";
        String endTime=time+"-31";
        QueryWrapper<Ordersetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("orderDate", startTime, endTime);
        List<Ordersetting> list = ordersettingMapper.selectList(queryWrapper);
        ArrayList<CalenderVo> calenderVoslist = new ArrayList<>();
        for (Ordersetting ordersetting : list) {
            Date date = ordersetting.getOrderdate();
            Calendar instance = Calendar.getInstance();
            instance.setTime(date);
            //得到今天是几号
            int day= instance.get(Calendar.DAY_OF_MONTH);
            calenderVoslist.add(new CalenderVo(day,ordersetting.getNumber(),ordersetting.getReservations()));
        }
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS,calenderVoslist);
    }

    @Override
    public Result update(Ordersetting ordersetting) {
        QueryWrapper<Ordersetting> queryWrapper = new QueryWrapper<>();
        //通过date查找
        queryWrapper.eq("orderDate", ordersetting.getOrderdate());
        //查询到数据库对应的对象
        Ordersetting selectOne = ordersettingMapper.selectOne(queryWrapper);
        if(selectOne!=null){
            //如果查到了进行修改
            //修改的number大于已经预约的数量
            if(ordersetting.getNumber()<selectOne.getReservations()){
                return new Result(false, "修改的数量不能小于已经预约人数");
            }
            selectOne.setNumber(ordersetting.getNumber());
            ordersettingMapper.updateById(selectOne);
        }else{
            //数据库中没有查找到，进行添加
            ordersettingMapper.insert(ordersetting);
        }
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }

}
